<?php
if( !defined("IN_IA") ) 
{
	exit( "Access Denied" );
}
class Diypage_UnporBabyModel{
	public function saveImg($str) 
	{
		if( empty($str) || is_array($str) ) 
		{
			return NULL;
		}
		$str = preg_replace_callback("/\"imgurl\"\\:\"([^\\'\" ]+)\"/", function($matches) 
		{
			$preg = (!empty($matches[1]) ? istripslashes($matches[1]) : "");
			if( empty($preg) ) 
			{
				return "\"imgurl\":\"\"";
			}
			if( !strexists($preg, "../addons/imfox_eos") ) 
			{
				$newUrl = save_media($preg);
			}
			else 
			{
				$newUrl = $preg;
			}
			return "\"imgurl\":\"" . $newUrl . "\"";
		}
		, $str);
		return $str;
	}
	public function getPage($id = 0, $mobile = false) 
	{
		global $_W;
		if( empty($id) ) 
		{
			
			return false;
		}
		
		$where = " WHERE uniacid=:uniacid";
		$params = array( ":uniacid" => $_W["uniacid"] );
		if( is_numeric($id) ) 
		{
			$where .= " AND id=:id";
			$params[":id"] = $id;
		}
		else 
		{
			if( $id == "member" ) 
			{
				$where .= " AND type = 3";
			}
			else 
			{
				if( $id == "goodsdetail" ) 
				{
					$where .= " AND type = 4";
				}
				else 
				{
					if( $id == "home" ) 
					{
						$where .= " AND (type = 2 || type = 20)";
					}
					else 
					{
						if( $id == "seckill" ) 
						{
							$where .= " AND type = 5";
						}
					}
				}
			}
			$where .= " AND isdefault=1";
		}
		$page = pdo_fetch("SELECT * FROM " . tablename("imfox_eos_diypage") . $where . " LIMIT 1 ", $params);
		if( !empty($page) ) 
		{
			if( $id == "goodsdetail" && empty($page["status"]) ) 
			{
				return false;
			}
			$page["data"] = base64_decode($page["data"]);
			
			$page["data"] = json_decode($page["data"], true);
			if( is_array($page["data"]["page"]) && !empty($page["data"]["page"]["icon"]) ) 
			{
				$page["data"]["page"]["icon"] = tomedia($page["data"]["page"]["icon"]);
			}
			if( !empty($page["data"]["items"]) ) 
			{
				foreach( $page["data"]["items"] as $itemid => &$item ) 
				{
					$item = $this->goodsData($item, $mobile);
					if( $mobile ) 
					{
						$item = $this->rpx($item);
						$item = $this->mediaData($item);
						if( $item["id"] == "richtext" ) 
						{
							if( is_array($item["params"]) && !empty($item["params"]["content"]) ) 
							{
								$item["params"]["content"] = base64_decode($item["params"]["content"]);
							}
						}
						else 
						{
							if( $item["id"] == "menu" && !empty($item["style"]["showtype"]) ) 
							{
								if( !empty($item["data"]) ) 
								{
									$swiperpage = (empty($item["style"]["pagenum"]) ? 8 : $item["style"]["pagenum"]);
									$data_temp = array( );
									$k = 0;
									$i = 1;
									foreach( $item["data"] as $childid => $child ) 
									{
										$data_temp[$k][] = $child;
										if( $i < $swiperpage ) 
										{
											$i++;
										}
										else 
										{
											$i = 1;
											$k++;
										}
									}
									$item["data_temp"] = $data_temp;
									$rownum = (empty($item["style"]["rownum"]) ? 4 : $item["style"]["rownum"]);
									$item["data_line"] = ceil($swiperpage / $rownum);
									unset($swiperpage);
									unset($data_temp);
									unset($k);
									unset($i);
								}
								else 
								{
									unset($page["data"]["items"][$itemid]);
								}
							}
							else 
							{
								if( $item["id"] == "picturew" && !empty($item["params"]["showtype"]) ) 
								{
									if( !empty($item["data"]) ) 
									{
										$swiperpage = (empty($item["style"]["pagenum"]) ? 2 : $item["style"]["pagenum"]);
										$data_temp = array( );
										$k = 0;
										$i = 1;
										foreach( $item["data"] as $childid => $child ) 
										{
											$data_temp[$k][] = $child;
											if( $i < $swiperpage ) 
											{
												$i++;
											}
											else 
											{
												$i = 1;
												$k++;
											}
										}
										$item["data_temp"] = $data_temp;
										unset($swiperpage);
										unset($data_temp);
										unset($k);
										unset($i);
									}
									else 
									{
										unset($page["data"]["items"][$itemid]);
									}
								}
								else 
								{
									if( $item["id"] == "notice" && empty($item["params"]["noticedata"]) ) 
									{
										$limit = (!empty($item["params"]["noticenum"]) ? $item["params"]["noticenum"] : 5);
										$notices = pdo_fetchall("select id, title, link, thumb from " . tablename("ewei_shop_notice") . " where uniacid=:uniacid and status=1 order by displayorder desc limit " . $limit, array( ":uniacid" => $_W["uniacid"] ));
										$item["data"] = array( );
										if( !empty($notices) && is_array($notices) ) 
										{
											foreach( $notices as $index => $notice ) 
											{
												$childid = rand(1000000000, 9999999999);
												$childid = "C" . $childid;
												$item["data"][$childid] = array( "id" => $notice["id"], "title" => $notice["title"], "linkurl" => "/pages/shop/notice/detail/detail?id=" . $notice["id"] );
											}
										}
									}
									else 
									{
										if( $item["id"] == "video" ) 
										{
											if( empty($item["params"]["videourl"]) ) 
											{
												unset($page["data"]["items"][$itemid]);
											}
											else 
											{
												$item["params"]["videourl"] = tomedia($item["params"]["videourl"]);
												if( strexists($item["params"]["videourl"], "v.qq.com/iframe/player.html") ) 
												{
													$videourl = $this->getQVideo($item["params"]["videourl"]);
													if( !is_error($videourl) ) 
													{
														$item["params"]["videourl"] = $videourl;
													}
												}
												$item["params"]["poster"] = tomedia($item["params"]["poster"]);
											}
										}
										else 
										{
											if( $item["id"] == "audio" ) 
											{
												if( empty($item["params"]["audiourl"]) ) 
												{
													unset($page["data"]["items"][$itemid]);
												}
												else 
												{
													$item["params"]["audiourl"] = tomedia($item["params"]["audiourl"]);
													$item["params"]["headurl"] = tomedia($item["params"]["headurl"]);
												}
											}
											else 
											{
												if( $item["id"] == "icongroup" ) 
												{
													if( empty($item["data"]) || !is_array($item["data"]) ) 
													{
														unset($page["data"]["items"][$itemid]);
													}
													foreach( $item["data"] as $childid => &$child ) 
													{
														if( empty($child["iconclass"]) ) 
														{
															unset($item["data"][$childid]);
														}
														// if( !empty($child["linkurl"]) ) 
														// {
														// 	$linkurl = $this->judge("url", $child["linkurl"]);
														// 	if( !$linkurl ) 
														// 	{
														// 		unset($item["data"][$childid]);
														// 	}
														// 	else 
														// 	{
														// 		$child["dotnum"] = $this->judge("dot", $child["linkurl"]);
														// 	}
														// }
													}
													unset($child);
												}
												else 
												{
													if( $item["id"] == "goods" ) 
													{
														if( !empty($item["data"]) ) 
														{
															if( !empty($item["params"]["goodsscroll"]) ) 
															{
																$swiperpage = 1;
																if( $item["style"]["liststyle"] == "block" ) 
																{
																	$swiperpage = 2;
																}
																else 
																{
																	if( $item["style"]["liststyle"] == "block three" ) 
																	{
																		$swiperpage = 3;
																	}
																}
																$data_temp = array( );
																$k = 0;
																$i = 1;
																foreach( $item["data"] as $childid => $child ) 
																{
																	$data_temp[$k][] = $child;
																	if( $i < $swiperpage ) 
																	{
																		$i++;
																	}
																	else 
																	{
																		$i = 1;
																		$k++;
																	}
																}
																$item["data_temp"] = $data_temp;
																unset($swiperpage);
																unset($data_temp);
																unset($k);
																unset($i);
															}
															if( $item["params"]["showicon"] == 1 ) 
															{
																if( $item["style"]["goodsicon"] == "recommand" ) 
																{
																	$item["style"]["goodsicon"] = "推荐";
																}
																else 
																{
																	if( $item["style"]["goodsicon"] == "hotsale" ) 
																	{
																		$item["style"]["goodsicon"] = "热销";
																	}
																	else 
																	{
																		if( $item["style"]["goodsicon"] == "isnew" ) 
																		{
																			$item["style"]["goodsicon"] = "新上";
																		}
																		else 
																		{
																			if( $item["style"]["goodsicon"] == "sendfree" ) 
																			{
																				$item["style"]["goodsicon"] = "包邮";
																			}
																			else 
																			{
																				if( $item["style"]["goodsicon"] == "istime" ) 
																				{
																					$item["style"]["goodsicon"] = "限时购";
																				}
																				else 
																				{
																					if( $item["style"]["goodsicon"] == "bigsale" ) 
																					{
																						$item["style"]["goodsicon"] = "促销";
																					}
																					else 
																					{
																						$item["params"]["showicon"] == 0;
																					}
																				}
																			}
																		}
																	}
																}
															}
															if( $item["params"]["saleout"] == 0 && !empty($_W["shopset"]["shop"]["saleout"]) ) 
															{
																$item["params"]["saleout"] = tomedia($_W["shopset"]["shop"]["saleout"]);
																if( empty($item["params"]["saleout"]) ) 
																{
																	$item["params"]["saleout"] = tomedia("../addons/imfox_eos/static/diypage/images/default/saleout-2.png");
																}
															}
															else 
															{
																if( $item["params"]["saleout"] == 1 && !empty($item["style"]["saleoutstyle"]) ) 
																{
																	$item["params"]["saleout"] = tomedia("../addons/imfox_eos/static/diypage/images/default/saleout-" . $item["style"]["saleoutstyle"] . ".png");
																}
															}
														}
														else 
														{
															unset($page["data"]["items"][$itemid]);
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
					else 
					{
						//$item = $this->goodsCG($item);
					}
				}
				if( !$mobile ) 
				{
				}
			}
		}
		return $page;
	}
	public function goodsData($item, $mobile = false) 
	{
		global $_W;
		// $member = m("member")->getMember($_W["openid"]);
		// $cansee = 1;
		// if( empty($member["isagent"]) || $member["status"] == 0 || $member["agentblack"] == 1 ) 
		// {
		// 	$cansee = 0;
		// }
		if( $item["id"] == "goods" ) 
		{
			if( empty($item["params"]["goodsdata"]) && !empty($item["data"]) && is_array($item["data"]) ) 
			{
				$goodsids = array( );
				foreach( $item["data"] as $index => $data ) 
				{
					if( !empty($data["gid"]) ) 
					{
						$goodsids[] = $data["gid"];
					}
				}
				if( !empty($goodsids) && is_array($goodsids) ) 
				{
					$goodsids = array_filter($goodsids);
					$newgoodsids = implode(",", $goodsids);
					$goods = pdo_fetchall("select id, title, subtitle, thumb, minprice, sales, salesreal, total, showlevels, showgroups, bargain,hascommission,nocommission,commission,commission1_rate,commission1_rate,marketprice,commission1_pay,maxprice, productprice, video,`type` from " . tablename("ewei_shop_goods") . " where id in( " . $newgoodsids . " ) and status=1 and deleted=0 and checked=0 and uniacid=:uniacid order by displayorder desc ", array( ":uniacid" => $_W["uniacid"] ));
				}
			}
			else 
			{
				if( $item["params"]["goodsdata"] == 1 && !empty($item["params"]["cateid"]) && $mobile ) 
				{
					$orderby = " displayorder desc, createtime desc";
					if( $item["params"]["goodssort"] == 1 ) 
					{
						$orderby = " sales+salesreal desc, displayorder desc";
					}
					else 
					{
						if( $item["params"]["goodssort"] == 2 ) 
						{
							$orderby = " minprice desc, displayorder desc";
						}
						else 
						{
							if( $item["params"]["goodssort"] == 3 ) 
							{
								$orderby = " minprice asc, displayorder desc";
							}
						}
					}
					$item["params"]["goodsnum"] = (!empty($item["params"]["goodsnum"]) ? $item["params"]["goodsnum"] : 20);
					$goodslist = m("goods")->getList(array( "cate" => $item["params"]["cateid"], "pagesize" => $item["params"]["goodsnum"], "page" => 1, "order" => $orderby ));
					$goods = $goodslist["list"];
				}
				else 
				{
					if( $item["params"]["goodsdata"] == 2 && !empty($item["params"]["groupid"]) && $mobile ) 
					{
						$group = pdo_fetch("SELECT * FROM " . tablename("ewei_shop_goods_group") . " WHERE id=:id and uniacid=:uniacid limit 1 ", array( ":id" => $item["params"]["groupid"], ":uniacid" => $_W["uniacid"] ));
						if( !empty($group) && !empty($group["goodsids"]) ) 
						{
							$orderby = " order by displayorder desc";
							if( $item["params"]["goodssort"] == 1 ) 
							{
								$orderby = " order by sales+salesreal desc, displayorder desc";
							}
							else 
							{
								if( $item["params"]["goodssort"] == 2 ) 
								{
									$orderby = " order by minprice desc, displayorder desc";
								}
								else 
								{
									if( $item["params"]["goodssort"] == 3 ) 
									{
										$orderby = " order by minprice asc, displayorder desc";
									}
								}
							}
							$item["params"]["goodsnum"] = (!empty($item["params"]["goodsnum"]) ? $item["params"]["goodsnum"] : 20);
							$goods = pdo_fetchall("select id, title, subtitle, thumb, minprice, sales, salesreal, total, showlevels, showgroups, bargain, productprice,hascommission,nocommission,commission,commission1_rate,commission1_rate,marketprice,commission1_pay,maxprice,video,`type` from " . tablename("ewei_shop_goods") . " where id in( " . $group["goodsids"] . " ) and status=1 and `deleted`=0 and `status`=1 and uniacid=:uniacid " . $orderby . " limit " . $item["params"]["goodsnum"], array( ":uniacid" => $_W["uniacid"] ));
						}
					}
					else 
					{
						if( 2 < $item["params"]["goodsdata"] && $mobile ) 
						{
							$args = array( "pagesize" => $item["params"]["goodsnum"], "page" => 1, "order" => " displayorder desc, createtime desc" );
							if( $item["params"]["goodssort"] == 1 ) 
							{
								$args["order"] = " sales+salesreal desc, displayorder desc";
							}
							else 
							{
								if( $item["params"]["goodssort"] == 2 ) 
								{
									$args["order"] = " minprice desc, displayorder desc";
								}
								else 
								{
									if( $item["params"]["goodssort"] == 3 ) 
									{
										$args["order"] = " minprice asc, displayorder desc";
									}
								}
							}
							if( $item["params"]["goodsdata"] == 3 ) 
							{
								$args["isnew"] = 1;
							}
							else 
							{
								if( $item["params"]["goodsdata"] == 4 ) 
								{
									$args["ishot"] = 1;
								}
								else 
								{
									if( $item["params"]["goodsdata"] == 5 ) 
									{
										$args["isrecommand"] = 1;
									}
									else 
									{
										if( $item["params"]["goodsdata"] == 6 ) 
										{
											$args["isdiscount"] = 1;
										}
										else 
										{
											if( $item["params"]["goodsdata"] == 7 ) 
											{
												$args["issendfree"] = 1;
											}
											else 
											{
												if( $item["params"]["goodsdata"] == 8 ) 
												{
													$args["istime"] = 1;
												}
											}
										}
									}
								}
							}
							$goodslist = m("goods")->getList($args);
							$goods = $goodslist["list"];
						}
					}
				}
			}
			if( !empty($goods) && is_array($goods) ) 
			{
				$level = $this->getLevel($_W["openid"]);
				$set = m("common")->getPluginset("commission");
				foreach( $goods as $key => $value ) 
				{
					if( $value["hasoption"] == 1 ) 
					{
						$pricemax = array( );
						$options = pdo_fetchall("select * from " . tablename("ewei_shop_goods_option") . " where goodsid=:goodsid and                               uniacid=:uniacid order by displayorder asc", array( ":goodsid" => $value["id"], ":uniacid" => $_W["uniacid"] ));
						foreach( $options as $k => $v ) 
						{
							array_push($pricemax, $v["marketprice"]);
						}
						$value["marketprice"] = max($pricemax);
					}
					if( $value["nocommission"] == 0 ) 
					{
						if( p("seckill") && p("seckill")->getSeckill($value["id"]) ) 
						{
							continue;
						}
						if( 0 < $value["bargain"] ) 
						{
							continue;
						}
						$goods[$key]["seecommission"] = $this->getCommission($value, $level, $set);
						if( 0 < $goods[$key]["seecommission"] ) 
						{
							$goods[$key]["seecommission"] = round($goods[$key]["seecommission"], 2);
						}
						$goods[$key]["cansee"] = $set["cansee"];
						$goods[$key]["seetitle"] = $set["seetitle"];
					}
					else 
					{
						$goods[$key]["seecommission"] = 0;
						$goods[$key]["cansee"] = $set["cansee"];
						$goods[$key]["seetitle"] = $set["seetitle"];
					}
				}
				if( empty($item["params"]["goodsdata"]) ) 
				{
					foreach( $item["data"] as $childid => $childgoods ) 
					{
						foreach( $goods as $index => $good ) 
						{
							if( $good["id"] == $childgoods["gid"] ) 
							{
								// if( $mobile && !m("goods")->visit($good, $this->member) ) 
								// {
								// 	continue;
								// }
								$item["data"][$childid] = array( "gid" => $good["id"], "title" => $good["title"], "subtitle" => $good["subtitle"], "price" => $good["minprice"], "thumb" => $good["thumb"], "total" => $good["total"], "productprice" => $good["productprice"], "ctype" => $good["type"], "sales" => $good["sales"] + intval($good["salesreal"]), "video" => $good["video"], "seecommission" => $good["seecommission"], "cansee" => ($cansee ? $good["cansee"] : $cansee), "seetitle" => $good["seetitle"], "bargain" => $good["bargain"] );
							}
						}
					}
				}
				else 
				{
					$item["data"] = array( );
					foreach( $goods as $key => $value ) 
					{
						if( $value["hasoption"] == 1 ) 
						{
							$pricemax = array( );
							$options = pdo_fetchall("select * from " . tablename("ewei_shop_goods_option") . " where goodsid=:goodsid and                               uniacid=:uniacid order by displayorder asc", array( ":goodsid" => $value["id"], ":uniacid" => $_W["uniacid"] ));
							foreach( $options as $k => $v ) 
							{
								array_push($pricemax, $v["marketprice"]);
							}
							$value["marketprice"] = max($pricemax);
						}
						if( $value["nocommission"] == 0 ) 
						{
							if( p("seckill") && p("seckill")->getSeckill($value["id"]) ) 
							{
								continue;
							}
							if( 0 < $value["bargain"] ) 
							{
								continue;
							}
							$goods[$key]["seecommission"] = $this->getCommission($value, $level, $set);
							if( 0 < $goods[$key]["seecommission"] ) 
							{
								$goods[$key]["seecommission"] = round($goods[$key]["seecommission"], 2);
							}
							$goods[$key]["cansee"] = $set["cansee"];
							$goods[$key]["seetitle"] = $set["seetitle"];
						}
						else 
						{
							$goods[$key]["seecommission"] = 0;
							$goods[$key]["cansee"] = $set["cansee"];
							$goods[$key]["seetitle"] = $set["seetitle"];
						}
					}
					foreach( $goods as $index => $good ) 
					{
						$childid = "C" . rand(1000000000, 9999999999);
						$item["data"][$childid] = array( "gid" => $good["id"], "title" => $good["title"], "subtitle" => $good["subtitle"], "price" => $good["minprice"], "thumb" => $good["thumb"], "total" => $good["total"], "productprice" => $good["productprice"], "ctype" => $good["type"], "seecommission" => $good["seecommission"], "cansee" => $good["cansee"], "seetitle" => $good["seetitle"], "sales" => $good["sales"] + intval($good["salesreal"]), "video" => $good["video"], "bargain" => $good["bargain"] );
					}
				}
			}
		}
		return $item;
	}
	public function rpx($item) 
	{
		$needHandle = array( "fontsize", "padding", "paddingtop", "paddingleft", "margintop", "marginleft", "height", "leftright", "bottom" );
		if( is_array($item) && is_array($item["style"]) ) 
		{
			foreach( $item["style"] as $key => &$val ) 
			{
				if( in_array($key, $needHandle) ) 
				{
					$val = intval($val) * 2;
				}
			}
			unset($key);
			unset($val);
		}
		return $item;
	}
	public function mediaData($item) 
	{
		$needHandle = array( "iconurl", "imgurl", "leftnavimg", "rightnavimg", "thumb", "goodsiconsrc" );
		if( is_array($item) && is_array($item["params"]) ) 
		{
			foreach( $item["params"] as $key => &$val ) 
			{
				if( in_array($key, $needHandle) ) 
				{
					$val = tomedia($val);
				}
			}
			unset($key);
			unset($val);
		}
		if( is_array($item) && is_array($item["data"]) && 0 < count($item["data"]) ) 
		{
			$newDatas = array( );
			foreach( $item["data"] as $childid => $child ) 
			{
				if( is_array($child) ) 
				{
					foreach( $child as $key => &$val ) 
					{
						if( in_array($key, $needHandle) ) 
						{
							$val = tomedia($val);
						}
						if( $key == "linkurl" ) 
						{
							if( strpos($val, "tel:") !== false ) 
							{
								$phone = explode(":", $val);
								$child["phone"] = $phone[1];
								unset($child["linkurl"]);
							}
							else 
							{
								if( strpos($val, "appid:") !== false ) 
								{
									if( strpos($val, ",") !== false ) 
									{
										$miniprogramdata = explode(",", $val);
										$appid = explode(":", $miniprogramdata[0]);
										$appurl = explode(":", $miniprogramdata[1]);
										$child["appid"] = $appid[1];
										$child["appurl"] = $appurl[1];
										unset($child["linkurl"]);
									}
									else 
									{
										$appid = explode(":", $val);
										$child["appid"] = $appid[1];
										unset($child["linkurl"]);
									}
								}
							}
						}
					}
					unset($key);
					unset($val);
				}
				$newDatas[] = $child;
			}
			$item["data"] = $newDatas;
		}
		if( $item["id"] == "richtext" ) 
		{
			$item = $this->mediaRich($item);
		}
		return $item;
	}
	public function getQVideo($url = NULL, $vid = false) 
	{
		if( empty($url) ) 
		{
			return NULL;
		}
		if( !$vid ) 
		{
			$vid = $this->getQVideoVid($url);
		}
		load()->func("communication");
		$request = ihttp_get("https://h5vv.video.qq.com/getinfo?callback=renrenVideo&otype=json&platform=11001&host=v.qq.com&sphttps=1&vid=" . $vid);
		if( empty($request) || $request["code"] != 200 || empty($request["content"]) ) 
		{
			return error(-1, "获取失败-1");
		}
		$content = $request["content"];
		$content = ltrim($content, "renrenVideo(");
		$content = rtrim($content, ")");
		$array = json_decode($content, true);
		if( !is_array($array) || !isset($array["vl"]) || !is_array($array["vl"]["vi"]) || !is_array($array["vl"]["vi"][0]) ) 
		{
			return error(-1, "获取失败-2");
		}
		$fvideo = $array["vl"]["vi"][0];
		if( empty($fvideo["fvkey"]) || !isset($fvideo["ul"]) || !is_array($fvideo["ul"]["ui"]) || !is_array($fvideo["ul"]["ui"][0]) || !isset($fvideo["ul"]["ui"][0]["url"]) ) 
		{
			return error(-1, "获取失败-3");
		}
		$videopath = (isset($fvideo["ul"]["ui"][1]) && !empty($fvideo["ul"]["ui"][1]["url"]) ? $fvideo["ul"]["ui"][1]["url"] : $fvideo["ul"]["ui"][0]["url"]);
		return $videopath . $fvideo["fn"] . "?vkey=" . $fvideo["fvkey"];
	}
	public function getQVideoVid($url) 
	{
		if( empty($url) || !strexists($url, "v.qq.com/iframe/") ) 
		{
			return NULL;
		}
		$vid = "";
		$params = parse_url($url);
		parse_str($params["query"]);
		return $vid;
	}
}